<template>
	<view class="content">
		<view class="container">
			<view class="cardBankName">
				<view class="item">
					<view>账户名称</view>
					<input type="text" placeholder="请输入姓名" v-model="accountName" />
				</view>
			</view>
			<view class="cardBankName">
				<view class="item">
					<view>开户银行</view>
					<input type="text" placeholder="请输入开户银行" v-model="bankName" />
				</view>
			</view>
			<view class="cardBankName">
				<view class="item">
					<view>支行名称</view>
					<input type="text" placeholder="请输入支行名称" v-model="branchName" />
				</view>
			</view>
			<view class="cardBankName">
				<view class="item">
					<view>银行账号</view>
					<input type="text" placeholder="请输入银行账号" v-model="bankAccount" />
				</view>
			</view>
		</view>
		<view v-if="!isEdit" class="addCards" @click="toAddCards('add')">
			保存账号
		</view>
		<view v-else class="addCards" @click="toAddCards('edit')">
			修改账号
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				accountName: '',
				bankName: '',
				branchName: '',
				bankAccount: '',
				isEdit: false,
				cardId: null
			};
		},
		onLoad(option) {
			if (option.cardId) {
				this.cardId = option.cardId
				this.isEdit = true
				this.$api.post({
					url: '/store/getBankCardById',
					data: {
						id: option.cardId
					},
					success: res => {
						this.accountName = res.account_name;
						this.bankName = res.card_name;
						this.branchName = res.branch_name;
						this.bankAccount = res.card_number;
					}
				})
			}
		},
		methods: {
			toAddCards(status) {
				if (status == 'add') {
					this.$api.post({
						url: "/store/addBankCard",
						data: {
							user_id: uni.getStorageSync('xyvenue:user').id, // 请根据实际情况设置 user_id 的值
							branch_name: this.branchName,
							card_number: this.bankAccount,
							account_name: this.accountName,
							card_name: this.bankName,
						},
						success: res => {
							uni.navigateBack(-1)
							uni.showToast({
								title: "绑定银行卡信息成功",
								icon: "none"
							})
						}
					})
				} else {
					this.$api.post({
						url: "/store/editBankCard",
						data: {
							id: this.cardId,
							user_id: uni.getStorageSync('xyvenue:user').id, // 请根据实际情况设置 user_id 的值
							branch_name: this.branchName,
							card_number: this.bankAccount,
							account_name: this.accountName,
							card_name: this.bankName,
						},
						success: res => {
							uni.navigateBack(-1)
							uni.showToast({
								title: "修改银行卡信息成功",
								icon: "none"
							})
						}
					})
				}
				// 清空输入框
				this.accountName = '';
				this.bankName = '';
				this.branchName = '';
				this.bankAccount = '';
			}
		}
	}
</script>

<style lang="less">
	.content {
		background-color: #F4F4F4;
		min-height: 100vh;

		.container {
			padding-top: 40rpx;
			width: 90%;
			margin: auto;
		}
	}

	.cardBankName {
		padding: 10rpx 30rpx;
		background-color: #fff;
		border-radius: 10rpx;
		margin: auto;
		margin-bottom: 40rpx;

		.item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 0;

			input {
				text-align: right;
			}
		}
	}

	.addCards {
		background-color: #65C61E;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 90%;
		left: 50%;
		transform: translateX(-50%);
		height: 85rpx;
		color: #fff;
		border-radius: 10rpx;
		position: fixed;
		bottom: 10%;
	}
</style>